<script setup lang="ts">
import {computed, inject, ref, watch,reactive} from "vue";
import AddModal from "@cps/actions/addModal.vue";
import shipment from '@/api/document/shipment'
import {getDictData} from "@/utils/crud/dictNew.js";
import AddModalForm from "@cps/actionsNew/addModalForm.vue";
import dictHelp from "@/utils/dicthelp";
const size = ref('medium');
// const crudRef = inject('crudRef')
// const crudForm = inject('crudForm')
const record = inject('record')
const getRecord = inject('getRecord')
const columnsDist = inject('columnsDist')//全部字典
const addModalRef = ref()
const addModalMormRef = ref()

// 收件人
const recipientData = computed(() => {
  // let code = '';
  // // let dict = crudRef.value.getColumnService().dictMap.get('address_lib_detail_id');
  // let dict = columnsDist.value.dicthelpApi_addressLibDetail.data
  // if (dict !== undefined && dict.length > 0) {
  //   dict.map(i => {
  //     if (i.value === record.value.address_lib_detail_id) {
  //       code = i.label
  //     }
  //   })
  // }
  // 用空格拼接 城市/州/邮编/国家

  const address = [
    record.value.city,
    record.value.state,
    record.value.postcode,
  ].filter(Boolean).join(' ')

  const map = {
    'address_lib_detail_id' : "地址库",
    'name' : "收件人",
    'company' : '公司',
    'address1' : '地址一',
    'address2' : '地址二',
    'address3' : '地址三',
    'city' : '城市/州/邮编',
    'phone' : '电话',
    'email' : '邮箱',
    'country' : '国家',
  }


  // 获取地址库字典
  // const list = [
  //   // record.value.address_lib_detail_id,
  //   code,
  //   record.value.name,
  //   record.value.company,
  //   record.value.address1,
  //   record.value.address2,
  //   record.value.address3,
  //   address,
  //   record.value.phone,
  //   record.value.email,
  //   record.value.country,
  // ]
  const result = []
  for (const mapKey in map) {
    if (record.value[mapKey]!='' && record.value[mapKey]!=null){
      if(mapKey == 'address_lib_detail_id'){
        let value = getDictData('attrApi', 'dicthelpApi_addressLibDetail', record.value.address_lib_detail_id, 7, columnsDist.value)
        result.push({label: map[mapKey], value: value})
      }else if(mapKey == 'city'){
        result.push({label: map[mapKey], value: address})        
      }else{
        result.push({label: map[mapKey], value: record.value[mapKey]})
      }
    } 
  }
  return result;
})
const shipperData = ref([])
const vatData = computed(()=>{
  const map = {
    'vat_number' : "VAT号",
    'ioss' : 'IOSS号',
    'eori' : 'EORI',
    
  }
  const list = []
  for (const mapKey in map) {
    if (record.value[mapKey]!='' && record.value[mapKey]!=null){
      list.push({label: map[mapKey], value: record.value[mapKey]})
    } 
  }
  return list;
})
const chargeData = computed(()=>{
  const map = {
    'taxwith' : "交税方式",
    'deliverywith' : '交货条款',
    'exportwith' : '报关方式',
    'importwith' : '清关方式',
    
  }
  const list = []
  for (const mapKey in map) {
    if (record.value[mapKey]){
      let value = getDictData('dictObj', mapKey, record.value[mapKey], 2, columnsDist.value)
      list.push({label: map[mapKey], value: value})
    } 
  }
  return list;
})

const supplierData = ref([
  // {label: '供应商服务', value: '(未指定供应商服务)'},
  // {label: '供应商收费重', value: '10'},
])
const moreData = computed(()=>{
  /*[
  {label: '关联单号', value: '关联单号'},
  {label: '参考号一', value: '参考号一'},
  {label: '参考号二', value: '参考号二'},
  {label: 'PO Number', value: 'PO Number'},
  {label: '申报币种', value: 'USD'},
  {label: '客户单号', value: 'FBA123456787'},
  {label: '销售代表', value: '测试用户'},
  {label: '内部备注', value: '内部备注'},
  {label: '备注', value: '备注'},
  {label: '库位', value: '库位'},
  {label: '购买保险', value: '是'},
  {label: '保价', value: '1'},
  {label: '投保币种', value: 'CAD'},
  {
    label: '属性', value: '带电\n' +
        '危险品\n' +
        '液体\n' +
        '粉末\n' +
        '膏体\n' +
        '敏感货\n' +
        '木制品\n' +
        '纺织品'
  },
  {label: '主品名', value: '主品名'},
  {label: '供应商单号', value: '供应商单号'},
  {label: '包装类型', value: '包裹'},
]*/
  const map = {
    'self_reference' : "关联单号",
    'reference_1' : '参考号一',
    'reference_2' : '参考号二',
    'po_number' : 'PO Number',
    'ext_number' : '扩展箱号',
    'declare_currency' : '申报币种', // 1
    'client_reference' : '客户单号',
    'internal_remark' : '内部备注',
    'remark' : '备注',
    'seller_id':'销售代表',
    'finance_id':'财务代表',
    'customer_service_id':'客服代表',
    // 'location' : '库位',
    // 'buy_insurance' : '购买保险',
    // 'insured_value' : '保价',
    // 'insured_currency' : '投保币种',
    'attrs' : '属性',
    'main_name' : '主品名', // 1
    // 'supplier_order_no' : '供应商单号', // 1
    // 'packaging_type' : '包装类型', // 1
  }
  const list = []
  for (const mapKey in map) {
    // if (record.value[mapKey] && record.value[mapKey].length){
    if (record.value[mapKey] && record.value[mapKey]!=''){
      let value = record.value[mapKey]
      if(mapKey == 'attrs'){
        let label = ''
        let dict = columnsDist.value.dictObj.data.ShipmentAttrs
          dict.map(i => {
              for(let index = 0 ; index < value.length ; index++){
                  if (value[index] == i.key) {
                    label += i.title + ' '
                  }
                }
          })
          value=label
      }
      if (mapKey == 'seller_id' || mapKey == 'finance_id' || mapKey == 'customer_service_id'){
         value = getDictData('attrApi', 'dicthelpApi_user', record.value[mapKey], 4, columnsDist.value)
      } 
      list.push({label: map[mapKey], value: value})
    } 
  }
  return list;
})

// const edit = ()=>crudRef.value.crudFormRef.edit(record.value)
//编辑
const edit=()=>{
  console.log('jjjjjjjjj',addModalMormRef.value)
  addModalMormRef.value.onShow(record.value)
}
//重新加载
const resetGetRecord=(e)=>{
    getRecord()
  }
const containerType = ()=>{
  addModalRef.value.open(reactive([
        {
            title: "单号",
            dataIndex: "container_about_id",
            addDefaultValue:record.value.container_id > 0 ?record.value.container_id : null,
            formType: "select",
            dict: {
              // 远程通用接口请求，新版代码生成都有一个 remote 接口
              url: 'document/waybill/distribution',
              // 指定组件接收的props
              props: {label: 'name', value: 'value'},
              params:{
                service_id:record.value.service_id,
              },
              translation: true,
            },
            commonRules: {required: true,message: "请选择单号"}
        },
        {
          dataIndex: "id",
          addDefaultValue:record.value.id,
          addDisplay:false,
          addDisabled:true
        }
    ]) , {
         viewType: 'modal',
      }, shipment.updateWeightVolume,'预配货',function name() {
        getRecord()
      })
}

const EndDeliveryType = ()=>{
  addModalRef.value.open(reactive([
      {
        title: "末端派送方式",
        dataIndex: "end_delivery_type",
        formType: "radio",
        addDefaultValue:record.value.end_delivery_type,
        type:'button',
        dict:{
          name: 'endDeliveryType',
          translation: true
        }
      },
      {
        dataIndex: "id",
        addDefaultValue:record.value.id,
        addDisplay:false,
        addDisabled:true
      }
  ]) , {
         viewType: 'modal',
      }, shipment.endDeliveryType,'末端派送',function name() {
        getRecord()
      })
}

const editSupplier = async () =>{
  addModalRef.value.open(reactive([
      {
        title: "供应商服务",
        dataIndex: "supplier_channel_id",
        formType: "cascader",
        dict: {url: 'logistics/supplier/channel?type=13', translation: true,},
        addDefaultValue:record.value.supplier_channel_id > 0 ? record.value.supplier_channel_id : null,
      },
      {
        title: "供应商收费重",
        dataIndex: "supplier_recharge_weight",
        formType: "input",
        addDefaultValue:record.value.supplier_recharge_weight > 0 ? record.value.supplier_recharge_weight : null,
      },
      {
        title: "三方单号",
        dataIndex: "supplier_third_no",
        formType: "input",
        addDefaultValue:record.value.supplier_third_no,
      },
      {
        title: "是否追踪路由",
        dataIndex: "supplier_third_route",
        formType: "radio",
        addDefaultValue:record.value.supplier_third_route,
        dict:{
          data:[
            {label:'是',value:1},
            {label:'否',value:0},
          ],
          translation: true,
        }
      },
      {
        dataIndex: "id",
        addDefaultValue:record.value.id,
        addDisplay:false,
        addDisabled:true
      }
  ]) , {
         viewType: 'modal',
      }, shipment.updateSupplierInfo,'供应商',function name() {
        getRecord()
      })
}

const editCharge = async () =>{
  addModalRef.value.open(reactive([
      {
        title: "计泡系数",
        dataIndex: "volume_weight_radix",
        formType: "input",
        addDefaultValue:record.value.volume_weight_radix > 0 ? record.value.volume_weight_radix : null,
        addDisplay:record.value.service.bit_control.includes(9) || record.value.service.bit_control.includes('9')
      },
      {
        title: "固定应收运费",
        dataIndex: "fix_total_recharge_money",
        formType: "input",
        addDefaultValue:record.value.fix_total_recharge_money > 0 ? record.value.fix_total_recharge_money : null,
      },
      {
        title: "固定收费重",
        dataIndex: "fix_recharge_weight",
        formType: "input",
        addDefaultValue:record.value.fix_recharge_weight > 0 ? record.value.fix_recharge_weight : null,
      },
      {
        title: "销售收费重",
        dataIndex: "sell_recharge_weight",
        formType: "input",
        addDefaultValue:record.value.sell_recharge_weight > 0 ? record.value.sell_recharge_weight : null,
      },
      {
        dataIndex: "id",
        addDefaultValue:record.value.id,
        addDisplay:false,
        addDisabled:true
      }
  ]) , {
         viewType: 'modal',
      }, shipment.updateCharge,'收费重',function name() {
        getRecord()
      })
}

const changeService = async () =>{
  addModalRef.value.open(reactive([
      {
        dataIndex: "admin_service_id",
        title: "配货服务",
        formType: "select",
        addDefaultValue:record.value.admin_service_id,
        dict:dictHelp.service
      },
      {
        dataIndex: "shipment_id",
        addDefaultValue:record.value.id,
        addDisplay:false,
        addDisabled:true
      }
  ]) , {
         viewType: 'modal',
      }, shipment.changeService,'设置',function name() {
        getRecord()
      })
}

const changeRoute = async () =>{
  addModalRef.value.open(reactive([
      {
        dataIndex: "route_about_waybill_user",
        title: "外部路由提单",
        formType: "select",
        addDefaultValue:record.value.route_about_waybill_user,
        dict:dictHelp.waybill
      },
      {
        dataIndex: "route_about_waybill_admin",
        title: "内部路由提单",
        formType: "select",
        addDefaultValue:record.value.route_about_waybill_admin,
        dict:dictHelp.waybill
      },
      {
        dataIndex: "shipment_id",
        addDefaultValue:record.value.id,
        addDisplay:false,
        addDisabled:true
      }
  ]) , {
         viewType: 'modal',
      }, shipment.changeRoute,'设置',function name() {
        getRecord()
      })
}

</script>

<template>
  <a-grid-item :span="18">
    <a-collapse :default-active-key="['1']" class="g-collapse">
      <a-collapse-item header="基础信息" key="1">
        <add-modal ref="addModalRef"></add-modal>
        <template #extra>
          <a-button-group>
            <a-button type="primary" size="small" @click.stop="edit" v-auth="['document:shipment:update']"><icon-edit/>编辑</a-button>
            <a-button type="primary" size="small" @click.stop="containerType()" v-auth="['document:shipment:updateWeightVolume']"><icon-edit/>提单？船期</a-button>
            <a-button type="primary" size="small" @click.stop="EndDeliveryType()" v-auth="['document:shipment:endDeliveryType']"><icon-edit/>末端派送</a-button>
            <a-button v-if="record.status > 1" type="primary" size="small" @click.stop="editCharge()"><icon-book/>费用信息</a-button>
            <a-button type="primary" size="small" @click.stop="editSupplier()" v-auth="['document:shipment:updateSupplierInfo']"><icon-book/>供应商</a-button>
            <a-button type="primary" size="small" @click.stop="changeService()" v-auth="['document:shipment:changeService']"><icon-book />配货服务</a-button>
            <a-button type="primary" size="small" @click.stop="changeRoute()" v-auth="['document:shipment:changeRoute']"><icon-book />路由展示</a-button>
<!--            <a-button type="primary" size="small" @click.stop=""><icon-send/>供应商信息</a-button>-->
<!--            <a-button type="primary" size="small" @click.stop="">VAT</a-button>-->
          </a-button-group>
        </template>
        <a-row class="basInfo" style="height: 270px;overflow: auto;">
          <a-col :span="6">
            <a-descriptions :data="recipientData" :size="size" title="收件人" :column="1"/>
          </a-col>
          <a-col :span="6" v-if="shipperData.length > 0">
            <a-descriptions :data="shipperData" :size="size" title="发件人" :column="1" :labelStyle="{display:'none'}"/>
          </a-col>
          <a-col :span="6" v-if="vatData.length > 0">
            <a-descriptions :data="vatData" :size="size" title="VAT信息" :column="1" />
          </a-col>
          <a-col :span="6" v-if="chargeData.length > 0">
            <a-descriptions :data="chargeData" :size="size" title="费用信息" :column="1"/>
          </a-col>
          <a-col :span="6" v-if="supplierData.length > 0">
            <a-descriptions :data="supplierData" :size="size" title="供应商信息" :column="1"/>
          </a-col>
          <a-col :span="6" v-if="moreData.length > 0">
            <a-descriptions :data="moreData" :size="size" title="更多" :column="1"/>
          </a-col>
        </a-row>
      </a-collapse-item>
    </a-collapse>
    <add-modal-form  @emitClick="resetGetRecord" ref="addModalMormRef"></add-modal-form>
  </a-grid-item>
</template>

<style scoped>

</style>